define(['jquery', 'bootstrap', 'backend', 'table', 'form','cookie'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'thm/message/index',
                    add_url: 'thm/message/add',
                    edit_url: 'thm/message/edit',
                    del_url: 'thm/message/del',
                    multi_url: 'thm/message/multi',
                    table: 'thm_message',
                }
            });

            var table = $("#table");

            // 初始化表格
            //电话
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        //{field: 'id', title: __('Id')},
                        {field: 'mapurl', title: __('位置'), formatter: Controller.api.formatter.url, operate:false},
                        {field: 'date', title: __('Date'), sortable:true, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'name', title: __('Name'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        //{field: 'mobile', title: __('Mobile'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        //{field: 'type', title: __('Type'), searchList: {"0":__('Type 0'),"1":__('Type 1')}, formatter: Table.api.formatter.normal},
                        {field: 'callnumber', title: __('Callnumber'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'callname', title: __('Callname'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'calltype', title: __('Calltype'), searchList: {"呼入":__('呼入'),"呼出":__('呼出'),"未接":__('未接'),"":__('')}, formatter: Table.api.formatter.normal},
                        {field: 'callduration', title: __('Callduration'), operate:false},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                    var myColumns=[];
                    $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                         myColumns.push(item.field);
                    });
                    $.cookie('fa_thm_message', JSON.stringify(myColumns), { expires: 365, path: '/'});
             });
             if($.cookie('fa_thm_message')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_thm_message')), function(i, item) {
                  table.bootstrapTable('hideColumn', item);
                });
             }

            // 为表格绑定事件
            Table.api.bindevent(table);

            setInterval(function () {
                $(".btn-refresh").trigger("click");
            }, 10000);//100分钟刷新一次

        },
        index2: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'thm/message/index2',
                    add_url: '',
                    edit_url: '',
                    del_url: '',
                    multi_url: 'thm/message/multi',
                    table: 'thm_message',
                }
            });

            var table = $("#table");

            //短信
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                showToggle: false,
                showExport: false,
                searchFormVisible: true,
                columns: [
                    [
                        //{checkbox: true},
                        //{field: 'id', title: __('Id')},
                        {field: 'date', title: __('Date'), sortable:true, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'name', title: __('Name'), operate: false},
                        {field: 'mobile', title: __('Mobile'), operate: false},
                        {field: 'smsname', title: __('Smsname'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'smsmsg', title: __('Smsmsg'), operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符'},
                        {field: 'smszfmobile', title: __('轉發手機門號'), operate: false},
                        //{field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 选择隐藏字段事件，使用cookie存入
            table.on('column-switch.bs.table', function (e, json) {
                var myColumns=[];
                $.each(table.bootstrapTable('getHiddenColumns'), function(i, item) {
                    myColumns.push(item.field);
                });
                $.cookie('fa_thm_message', JSON.stringify(myColumns), { expires: 365, path: '/'});
            });
            if($.cookie('fa_thm_message')){
                //读取cookie隐藏字段
                $.each(JSON.parse($.cookie('fa_thm_message')), function(i, item) {
                    table.bootstrapTable('hideColumn', item);
                });
            }

            // 为表格绑定事件
            Table.api.bindevent(table);

            setInterval(function () {
                $(".btn-refresh").trigger("click");
            }, 10000);//100分钟刷新一次

        },
        sendmsg: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                url: function (value, row, index) {
                    return '<a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a>';
                }
            }
        }
    };
    return Controller;
});